/*
 *  Licensed to the Apache Software Foundation (ASF) under one

 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

package org.apache.directory.studio.persistence.core;


import java.util.Arrays;
import java.util.List;


public class JavaUtils
{
    // Array of Java reserved words up to Java 6
    public static final String[] keywords =
        { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "continue", "default",
            "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "if", "implements",
            "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected",
            "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw",
            "throws", "transient", "true", "try", "void", "volatile", "while" };


    /**
     * Compute the valid identifier from a given string by removing illegal
     * characters if it is not a Java reserved word.
     * 
     * @param value
     *            the value of the string
     * @return the valid value from the given value if it can be existed
     * @throws IllegalArgumentException
     *             if the given string is null
     */
    public static String getValidValue( String value ) throws IllegalArgumentException
    {
        StringBuffer validValue = new StringBuffer();
        if ( value == null )
        {
            throw new IllegalArgumentException();
        }
        else if ( value.length() > 1 && !( isKeyword( value ) ) )
        {
            char[] chars = value.toCharArray();
            boolean isChecked = false;
            for ( int i = 1; i < chars.length; i++ )
            {
                if ( isChecked == false && ( Character.isJavaIdentifierStart( chars[0] ) ) )
                {
                    validValue.append( chars[0] );
                    isChecked = true;
                }
                if ( Character.isJavaIdentifierPart( chars[i] ) )
                {
                    validValue.append( chars[i] );
                }
            }
        }
        else if ( value.length() == 0 )
        {
            validValue.append( "" );
        }
        else if ( value.length() == 1 && Character.isJavaIdentifierStart( value.charAt( 0 ) ) )
        {
            validValue.append( value.charAt( 0 ) );
        }
        return validValue.toString();
    }


    /**
     * check if a given string is a Java reserved word or not.
     * 
     * @param value
     *            the value of the string to be checked
     * @return true if the given value is a Java reserved word, false if not
     * 
     */
    public static boolean isKeyword( String value )
    {
        boolean isKeyword = false;
        if ( Arrays.asList( keywords ).contains( value ) )
        {
            isKeyword = true;
        }
        return isKeyword;
    }


    /**
     * Check if same Java identifier is repeated
     * 
     * @param javaMembers
     *            is a list of Java identifiers
     * @param value
     *            is a value to be checked with the list of identifiers
     * @return true if collides and false if not
     */
    public static boolean isCollide( List<String> javaMembers, String value )
    {
        boolean isCollide = false;
        if ( javaMembers.contains( value ) )
        {
            isCollide = true;
        }
        return isCollide;
    }


    /**
     * Check if a given string is a valid Java identifier or not both according
     * to the existence of legal characters and reserved words.
     * 
     * @param value
     *            string value to be checked for validness
     * @return true if valid else false
     */
    public static boolean isValid( String value )
    {
        boolean isValid = true;
        if ( getValidValue( value ).equals( value ) && !( value.length() == 0 ) )
        {
            isValid = true;
        }
        else
        {
            isValid = false;
        }
        return isValid;
    }
}
